
var goodsList = new Array();
var idCounter = 0;
function goodsOrderHandlerMain() {
	//注册选择发生改变的时候
	switcher();

	refreshTips();

	OrderButtonEvent();

	RemoveButtonEvent();

	$(".ChangeTotalButton").live("click", function() {

		var goodsIndex = $(this).val();
		var goodsInfo = goodsList[goodsIndex];

		goodsInfo.orderTotal = $(this).prev("input").val();
		return false;
	});

	goodsInfoChanged();

	SubmitButton();
	
	
	$(".textBox").live("blur",function(){
		
		var val = $(this).val();
	     var max =$(this).attr("max");
	     var min = $(this).attr("min");
	     
		if(   max==undefined)
			return ;
		if(min ==undefined)
			min=1;
		 
		max = parseFloat(max);
		min = parseFloat(min);
	 
		if($.trim(val).length==0 || isNumber(val)==false ||(val>max) || val<min ) 
		{  
			$(this).val(min);
			showErrorInfo("数量输入有误!");
			return;	
		}
		else
		{ 
			var goodsIndex = $(this).attr("oid");
 
			var goodsInfo = goodsList[ getGoodsIndexById(goodsIndex)];

			goodsInfo.orderTotal = val;
			 
			return false;
		};
		
	});
}

function refreshTips() {
	$("#youHavedGoods").html(
			"Now You have ordered: " + goodsList.length + " goods.");
}

function OrderButtonEvent() {
	$(".OkButton").click( function() {
		var identity = ++idCounter;

		var selectedGoods = $("#GoodsListSelector option:selected");
		var goodsId = selectedGoods.val();
		var gName = selectedGoods.text();
		var total = selectedGoods.attr("total");
		var remain = selectedGoods.attr("remain");
		var newGoods = {
			Identity :identity,
			goodsId :goodsId,
			goodsName :gName,
			goodsCategory :$("#GoodsTypeSelector").val(),
			categoryName :"",
			total :0,
			remain :0,
			postData :new Date()
		};
		newGoods.total = total;
		newGoods.onStore = remain;
		newGoods.categoryName = $("#GoodsTypeSelector option:selected").text();
		newGoods.orderTotal = $("#orderTotalInputBox").val();
		var exists = isContain(goodsId);

		if (goodsId == null) {
			showErrorInfo("请先选择一种物资类型。", true); // ("normal");
			// //.show().delay("2000").hide();
			return;
		}

		if (exists == true) {
			showErrorInfo("该类型的物资已经在您的申请列表中。", true); // .show().delay("2000").hide();
			return;
		}
		if (checkOrderTotal() == false)
			return;
		// alert(newGoods.goodsId);
		goodsList.push(newGoods);

		var newsGoodsHtml = buildGoodInfoItem(newGoods, 1);

		$("#goodsInfoHtmlList").append(newsGoodsHtml);
		refreshTips();
	});
}

function showErrorInfo(errorMsg, removeTips) {
	$("#marker").html(errorMsg).slideDown("normal").delay(3000).slideUp("slow");
	if (removeTips == true) {
		$("#goodsOrderingTips").hide();
	}
}

function RemoveButtonEvent() {
	$(".delete-btn").live("click", function() {
		var isToDelete = confirm("确实要移除吗?");
		if (isToDelete == true) {
			// delete
			var goodsId = $(this).attr("value");

			var goodsIndex = getGoodsIndexById(goodsId);
			// remove this one..
			goodsList.splice(goodsIndex, 1);

			// remove
			$("#tr_OrderInfo_" + goodsId).remove().die();
			showErrorInfo("移除了一项物资。", false);
			refreshTips();
			return false;
		}
	});
}

function getGoodsIndexById(goodsId) {
	var length = goodsList.length;
	var result = -1;
	for ( var i = 0; i < length; i++) {
		if (goodsList[i].goodsId == goodsId) {
			result = i;
			break;
		}
	}
	return result;
	// });
}

//
function switcher() {
	$("#GoodsTypeSelector").change( function() {
		var typeId = $(this).val();
		$.post("goodsOrder!queryByCategory.action", {
			"categoryId" :typeId
		}, function(data) {
			//对应的物资信息添加到select中
		
			$("#GoodsListSelector").html(data );
			var remain = $("#GoodsListSelector option:first").attr("remain");
			$("#goodsTotalCanbeOrdered").text(remain);
			// refreshTips();
				$("#goodsOrderingTips").show();
			},
			"html"
		);
		});

	}
	

	
	




function goodsInfoChanged() {
	$("#GoodsListSelector").change( function() {
		
		var item = $("#GoodsListSelector option:selected");
		var total = item.attr("total");
		var remain = item.attr("remain");
		$("#orderTotalInputBox").attr("max", remain);

		$("#goodsTotalCanbeOrdered").text(remain);
		$("#goodsOrderingTips").show();
	});
}

function isContain(goodsId) {
	var index = getGoodsIndexById(goodsId);

	return index > -1 ? true : false;
}

var testGoods = null;
// 提交
function SubmitButton() {
	$(".SubmitButton").click( function() {
		var json = $.toJSON(goodsList);
		submitHandler(json);
	});
	// post ..to server.
	//
}


// 生成 GoodsInfo Html Row
function buildGoodInfoItem(itemInfo, key) {
	if (key == 1) {
		var orderId = itemInfo.orderId;
		var goodsName = itemInfo.goodsName;
		var goodsId = itemInfo.goodsId;
		var totalGoods = itemInfo.total;
		var orderTotal = itemInfo.orderTotal;
		var remain = itemInfo.onStore;
		var categoryName = itemInfo.categoryName;
		var templete = '<tr id="tr_OrderInfo_'
				+ goodsId
				+ '"><td width="16" class="td_width25">&nbsp;</td><td width="170">'
				+ '<span id="txt_OrderInfo_CategoryName" >'
				+ categoryName
				+ '&nbsp;</span></td><td>'
				+ '<span id="txt_OrderInfo_Name">'
				+ goodsName
				+ '&nbsp;</span></td><td>'
				+ '<span id="txt_OrderInfo_Total">'
				+ totalGoods
				+ '&nbsp;</span> /<span class="red-tips" id="txt_OrderInfo_Remain">&nbsp;'
				+ remain
				+ '</span>'
				+ '</td><td>'+'<input id="editor_'
				+ goodsId
				+ '" class="textBox easyui-numberbox" type="text" style="width: 70px;"'
				+ 'maxlength="10" oid="'
				+ goodsId
				+ '" min="1" max="'
				+ remain
				+ '" value="'+orderTotal+'"/>'
				+ '  </td>'
				+ '<td><a href="javascript:void(0);" class="delete-btn" title="移除" onclick="" value="'
				+ goodsId
				+ '"><img alt="移除" src="themes/icons/delete.png"/></a></td></tr>';
	 
		return templete;
	} else {
		var orderId = itemInfo.orderId;
		var goodsName = itemInfo.goodsName;
		var goodsId = itemInfo.goodsId;
		var totalGoods = itemInfo.total;
		var orderTotal = itemInfo.orderTotal;
		var remain = itemInfo.onStore;
		var categoryName = itemInfo.categoryName;
		var templete = '<tr id="tr_OrderInfo_'
				+ goodsId
				+ '"><td width="16" class="td_width25">&nbsp;</td><td width="170">'
				+ '<span id="txt_OrderInfo_CategoryName" >'
				+ categoryName
				+ '&nbsp;</span></td><td>'
				+ '<span id="txt_OrderInfo_Name">'
				+ goodsName
				+ '&nbsp;</span></td><td>'
				+ '<span id="txt_OrderInfo_Total">'
				+ totalGoods
				+ '&nbsp;</span> /<span class="red-tips" id="txt_OrderInfo_Remain">&nbsp;'
				+ remain
				+ '</span>'
				+ '</td><td>'
				+ '<input id="editor_'
				+ goodsId
				+ '" class="textBox easyui-numberbox" type="text" style="width: 70px;"'
				+ 'maxlength="10" oid="'
				+ goodsId
				+ '" min="1" max="'
				+ remain
				+ '" value="'+orderTotal+'"/>'
				+ ' </td></tr>';
	
		return templete;
	}
}

// 验证申请数量是否超过剩余数量
function checkOrderTotal() {
	var sender = $("#orderTotalInputBox");
	var max = sender.attr("max");
	var val = sender.val() | 0;
	sender.focus();
	return checkMaxOrderNum(max, val);
}

function checkMaxOrderNum(max, val) {
	if (val <= 0) {
		showErrorInfo("请输入物资数量。", false);
		return false;
	}
	if (max < val) {
		showErrorInfo("您申请的数量最多为: " + max, false);
		return false;
	}
}

// for front-end
function validationInput(sender) {
	var max = $(sender).attr("max");
	// alert(max);
	var val = sender.val() | 0;
	var isOK = checkMaxOrderNum(max, val);
	return isOK;
}

function submitChanged(goodsId, newVal) {
	var index = getGoodsIndexById(goodsId);
	goodsList[index].orderTotal = newVal;
}
